package com.meihuayishu.backend.controller;

import com.meihuayishu.backend.common.ApiResponse;
import com.meihuayishu.backend.dto.UserDTO;
import com.meihuayishu.backend.dto.UserLoginDTO;
import com.meihuayishu.backend.dto.UserRegistrationDTO;
import com.meihuayishu.backend.dto.UserDivinationSettingsDTO;
import com.meihuayishu.backend.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "用户接口", description = "用户注册、登录和信息管理")
public class UserController {
    
    private final UserService userService;
    
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "注册新用户")
    public ApiResponse<UserDTO> register(@Valid @RequestBody UserRegistrationDTO registrationDTO) {
        try {
            UserDTO userDTO = userService.register(registrationDTO);
            return ApiResponse.success(userDTO);
        } catch (Exception e) {
            log.error("用户注册失败", e);
            return ApiResponse.error(400, e.getMessage());
        }
    }
    
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录验证")
    public ApiResponse<UserDTO> login(@Valid @RequestBody UserLoginDTO loginDTO) {
        try {
            UserDTO userDTO = userService.login(loginDTO);
            return ApiResponse.success(userDTO);
        } catch (Exception e) {
            log.error("用户登录失败", e);
            return ApiResponse.error(400, e.getMessage());
        }
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户信息")
    public ApiResponse<UserDTO> getUserById(@PathVariable Long id) {
        return userService.getUserById(id)
            .map(ApiResponse::success)
            .orElse(ApiResponse.error(404, "用户不存在"));
    }
    
    @PutMapping("/{id}/mood")
    @Operation(summary = "更新心情状态", description = "更新用户心情状态")
    public ApiResponse<UserDTO> updateMoodStatus(@PathVariable Long id, @RequestParam String moodStatus) {
        try {
            UserDTO userDTO = userService.updateMoodStatus(id, moodStatus);
            return ApiResponse.success(userDTO);
        } catch (Exception e) {
            log.error("更新心情状态失败", e);
            return ApiResponse.error(400, e.getMessage());
        }
    }
    
    @PutMapping("/{id}/city")
    @Operation(summary = "更新当前城市", description = "更新用户当前城市")
    public ApiResponse<UserDTO> updateCurrentCity(@PathVariable Long id, @RequestParam String currentCity) {
        try {
            UserDTO userDTO = userService.updateCurrentCity(id, currentCity);
            return ApiResponse.success(userDTO);
        } catch (Exception e) {
            log.error("更新当前城市失败", e);
            return ApiResponse.error(400, e.getMessage());
        }
    }
    
    @GetMapping("/{id}/divination-settings")
    @Operation(summary = "获取用户占卜设置", description = "获取用户的个性化占卜设置")
    public ApiResponse<UserDivinationSettingsDTO> getUserDivinationSettings(@PathVariable Long id) {
        try {
            UserDivinationSettingsDTO settings = userService.getUserDivinationSettings(id);
            return ApiResponse.success(settings);
        } catch (Exception e) {
            log.error("获取用户占卜设置失败", e);
            return ApiResponse.error(400, e.getMessage());
        }
    }
    
    @PutMapping("/{id}/divination-settings")
    @Operation(summary = "更新用户占卜设置", description = "更新用户的个性化占卜设置")
    public ApiResponse<UserDivinationSettingsDTO> updateUserDivinationSettings(
            @PathVariable Long id, 
            @RequestBody UserDivinationSettingsDTO settings) {
        try {
            settings.setUserId(id); // 确保使用路径中的用户ID
            UserDivinationSettingsDTO updatedSettings = userService.updateUserDivinationSettings(id, settings);
            return ApiResponse.success(updatedSettings);
        } catch (Exception e) {
            log.error("更新用户占卜设置失败", e);
            return ApiResponse.error(400, e.getMessage());
        }
    }
}